<template>
	<el-dialog
		v-model="visible"
		:close-on-click-modal="false"
		:title="dailogType == 'add' ? '新增' : dailogType == 'edit' ? '修改' : '详情'"
		draggable
		width="30%"
	>
		<el-form ref="dataFormRef" v-loading="loading" :model="form" :rules="dataRules" label-width="90px">
			<el-row :gutter="24" v-if="dailogType == 'detail' || dailogType == 'edit'">
				<el-col :span="24" class="mb20">
					<el-form-item label="产品Key:" prop="productKey">
						<el-input disabled v-model="form.productKey" placeholder="请输入产品Key" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="产品名称:" prop="name">
						<el-input :disabled="dailogType == 'detail'" v-model="form.name" placeholder="请输入产品名称" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="所属品类:" prop="category">
						<el-select :disabled="dailogType == 'detail'" v-model="form.category" placeholder="请选择产品">
							<el-option v-for="item in props.categoryData" :key="item.id" :label="item.name" :value="item.identify" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="所属供应商:" prop="supplierCode">
						<el-select :disabled="dailogType == 'detail'" v-model="form.supplierCode" placeholder="请选择供应商">
							<el-option v-for="item in props.supplierData" :key="item.supplierCode" :label="item.supplierName" :value="item.supplierCode" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="节点类型:" prop="nodeType">
						<el-radio-group :disabled="dailogType == 'detail'" v-model="form.nodeType">
							<el-radio v-for="(item, index) in props.nodeTypeData" :key="index" :label="item.value">{{ item.label }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="接入方式:" prop="accessWay">
						<el-select :disabled="dailogType == 'detail'" v-model="form.accessWay" placeholder="请选择接入方式">
							<el-option v-for="item in props.accessWayData" :key="item.id" :label="item.label" :value="item.value" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="发布状态:" prop="isPublish">
						<el-radio-group :disabled="dailogType == 'detail'" v-model="form.isPublish">
							<el-radio :label="'0'">未发布</el-radio>
							<el-radio :label="'1'">已发布</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="产品图片:" prop="img">
						<upload-img v-model:image-url="form.img" :disabled="dailogType == 'detail'" width="100px" height="100px" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
				<el-button type="primary" v-show="dailogType != 'detail'" @click="onSubmit" :disabled="loading">{{
					$t('common.confirmButtonText')
				}}</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script lang="ts" name="productFormDailog" setup>
import { defineProps } from 'vue';
import { useMessage } from '/@/hooks/message';
import { addObj, editObj, getObj } from '/@/api/basiciot/device/product';
import { useI18n } from 'vue-i18n';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义父组件传过来的值
const props = defineProps({
	categoryData: {
		type: Array,
		default: () => [],
	},
	nodeTypeData: {
		type: Array,
		default: () => [],
	},
	accessWayData: {
		type: Array,
		default: () => [],
	},
	supplierData: {
		type: Array,
		default: () => [],
	}
});

const { t } = useI18n();

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
//弹窗类型
const dailogType = ref('add');

// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	nodeType: '0',
	accessWay: 'MQTT',
	supplierCode: '',
	isPublish: '1',
	img: '',
});
// 定义校验规则
const dataRules = ref({
	productKey: [{ required: true, message: '产品Key不能为空', trigger: 'blur' }],
	name: [{ required: true, message: '产品名称不能为空', trigger: 'blur' }],
	category: [{ required: true, message: '所属品类不能为空', trigger: 'blur' }],
	accessWay: [{ required: true, message: '接入方式不能为空', trigger: 'blur' }],
	isPublish: [{ required: true, message: '是否发布不能为空', trigger: 'blur' }],
});



// 打开弹窗
const openDialog = (row: any, type: string) => {
	visible.value = true;
	dailogType.value = type;
	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
	if (type === 'edit' || type === 'detail') {
		getData(row.id);
	} else {
		form.id = '';
	}
};

const getData = (id: string) => {
	loading.value = true;
	getObj(id)
		.then((res) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};
// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	loading.value = true;

	try {
		if (form.id) {
			await editObj(form);
			useMessage().success(t('common.editSuccessText'));
		} else {
			await addObj(form);
			useMessage().success(t('common.addSuccessText'));
		}
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
